package com.tssup.fesco.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

import com.tssup.fesco.common.enums.YesNo;
import com.tssup.fesco.sales.goods.persistent.mapper.RSalesCatalogMapper;
import com.tssup.fesco.sales.goods.persistent.pojo.RSalesCatalog;
import com.tssup.fesco.search.dao.ESClient;
import com.tssup.fesco.search.vo.QueryGoodsCategoryVo;
import com.tssup.fesco.service.UpdateCatalogService;
import com.tssup.fesco.util.TaskLogger;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * Created by jiashen on 17-6-12.
 */
@Service
public class UpdateCatalogServiceImpl implements UpdateCatalogService {
    private final String indexName;
    private final String catalogTypeName;
    private final String CATALOG_ID = "0";
    private final Client client;

    @Autowired
    private TaskLogger taskLogger;

    @Autowired
    private RSalesCatalogMapper salesCatalogMapper;

    public UpdateCatalogServiceImpl(Environment env, ESClient esClient) {
        super();
        indexName = env.getProperty("elasticsearch.searchArgs.indexName");
        catalogTypeName = env.getProperty("elasticsearch.searchArgs.catalogTypeName");
        client = esClient.getESClient();
    }

    @Override
    public void updateAllCatalog() throws Exception {
        taskLogger.info("Updating all catalog...");
        List allCatalogList = queryCategorys(-1, null);
        String allCatalogJson = JSON.toJSONString(allCatalogList);
        IndexRequest indexRequest = new IndexRequest(indexName, catalogTypeName, CATALOG_ID)
            .source(jsonBuilder()
                .startObject()
                .field("catalog_id", CATALOG_ID)
                .field("catalog_str", allCatalogJson)
                .endObject());
        UpdateRequest updateRequest = new UpdateRequest(indexName, catalogTypeName, CATALOG_ID)
            .doc(jsonBuilder()
                .startObject()
                .field("catalog_id", CATALOG_ID)
                .field("catalog_str", allCatalogJson)
                .endObject())
            .upsert(indexRequest);
        client.update(updateRequest).get();
        taskLogger.info("Update all catalog completed.");

    }

    private List<QueryGoodsCategoryVo> queryCategorys(Integer parentId, Integer catalogType){
        QueryGoodsCategoryVo categoryVo;
        List<QueryGoodsCategoryVo> categoryVoList = new ArrayList<>();

        List<RSalesCatalog> catalogList = salesCatalogMapper.selectByParentIdAndCatalogType(parentId, catalogType);
        if(!CollectionUtils.isEmpty(catalogList)){
            for (RSalesCatalog salesCatalog : catalogList) {
                categoryVo = new QueryGoodsCategoryVo();
                categoryVo.setId(salesCatalog.getCatalogId());
                categoryVo.setCatalogName(salesCatalog.getCatalogName());
                categoryVo.setParentId(salesCatalog.getParentId());

                // 假如分类已经被禁用就不再遍历子分类了
                if(YesNo.NO.getValue().equals(salesCatalog.getIsEnd()) && Integer.valueOf(1).equals(salesCatalog.getIsShow())){
                    categoryVo.setChildren(queryCategorys(salesCatalog.getCatalogId(), catalogType));
                }
                if (Integer.valueOf(1).equals(salesCatalog.getIsShow())) {
                    categoryVoList.add(categoryVo);
                }
            }
        }
        return categoryVoList;
    }
}
